<?php
/**
 * 订单管理controller
 * author:ty
 * date:2017/09/11
*/
namespace App\Http\Controllers;

use App\Http\Models\Order;
use App\Http\Models\Shopcart;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;

class OrderController extends Controller
{
    public $cart = null;
    public $order = null;
    public function __construct()
    {
        $this->cart = new Shopcart();
        $this->order = new Order();
    }

    /**制单购物车过来订单*/
    public function cartorder()
    {
        $input = Input::all();
        $userid = session('user')->id;
        $addr_id =  DB::table('address')->where('addr_user_id',$userid)->value('addr_id');
        if(trim(Input::get('quota_current',null))-trim(Input::get('quotapay'))>0){
            $orders_quota=trim(Input::get('quotapay'));
        }else{
            $orders_quota = trim(Input::get('quota_current',null));
        }

        $param = [
            'cartid' => $input['mycart'],
            'userid' => session('user')->id,
            'orders_user_name' => session('user')->name,
            'orders_addr_id' => $addr_id,
            'orders_deposite'=>trim(Input::get('rentbond')),//新增押金
            'orders_rentbill'=>trim(Input::get('rentbill')),//新增租金
            'orders_count_bond' => trim(Input::get('quotapay',null)),//订单所需信用额度
            'orders_quota' => $orders_quota,/**使用信用额度*/
            'orders_discount' => trim(Input::get('coupon',null)),
            'user_coupon_id' => trim(Input::get('user_coupon_id','')),//用户优惠券数据id
            'coupon_dtid'=>trim(Input::get('coupon_dtid','')),//使用优惠券id
            'orders_user_require' => trim($input['orders_user_require']),
            'orders_status' => 0,
            'orders_amount' => trim(Input::get('realpay',null)),
            'created_at' => date('Y-m-d H:i:s',time()),
            'updated_at' => date('Y-m-d H:i:s',time())
        ];
        $res = $this->order->cartorder($param);
        $url = url('/paybill');
        if($res['res'] != 'ok'){
            $result = ['res'=>'error','msg'=>'下单失败!','detail'=>$res];
            return json_encode($result);
        }else{
            $result = ['res'=>'ok','msg'=>'下单成功!请去支付!','redirect'=>$url,'id'=>$res['msg']];
            return json_encode($result);
        }
    }

}
